package com.ivan.user.controller.api;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.alibaba.fastjson.JSONObject;
import com.ivan.common.utils.HttpClientUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 * @Author      Ivan
 * @Date        2023/4/7 13:31
 * @Description WxLoginController
 */
@Api(tags = "微信登录接口")
//@CrossOrigin//使用gateway后做了配置
@Controller
@RequestMapping("/api/wx/login")
public class WxLoginController {

    @Autowired
    private WxLoginConfig wxLoginConfig;

    @ApiOperation(value = "生成二维码")
    @GetMapping(value = "/qrCode")
    public void getQrCode(@RequestParam(value = "isRememberMe",required = false,defaultValue = "1")String isRememberMe, @RequestParam(value = "sign") String sign, HttpServletRequest request, HttpServletResponse response) throws IOException {
            String url = wxLoginConfig.getQrCodeUrl().replace("APPID",wxLoginConfig.getAppId())
            .replace("REDIRECT_URL",wxLoginConfig.getServer() + "/wx/login/callback")
            .replace("STATE","自定义传参");
        //生成二维码的，扫描后跳转上面的REDIRECT_URL地址
        QrCodeUtil.generate(url, 300, 300, "jpg", response.getOutputStream());
    }

    @ApiOperation(value = "回调")
    @GetMapping(value = "/callback")
    @ResponseBody
    public String pcCallback(String code, String state) throws Exception {
            String openId = getOpenId(code);
        System.out.println(openId);
        //获取微信用户信息
        getUserInfo(openId);
        //业务处理...
        return "登录成功";
    }

    /**
     *	获取openId
     * @param code
     * @return
     */
    private String getOpenId(String code){
            String url = wxLoginConfig.getOpenIdUrl().replace("APPID", wxLoginConfig.getAppId())
            .replace("APPSECRET", wxLoginConfig.getAppSecret()).replace("CODE",code);
        String res = null;
            try {
            res = HttpClientUtils.get(url);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        JSONObject object = JSONObject.parseObject(res);
        return object.getString("openid");
    }

    /**
     * 获取用户信息
     * @param openId
     * @return
     */
    private JSONObject getUserInfo(String openId){
            //从微信上中拉取用户信息
//            String url = wxLoginConfig.getUserInfoUrl().replace("ACCESS_TOKEN",getAccessToken()).replace("OPENID",openId);
//        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
//        String result = forEntity.getBody();
//        JSONObject jsonObject = JSONObject.parseObject(result);
//        return jsonObject;
        return null;
    }
}
